package com.devops.admin.util;

import com.devops.common.exception.HandleDataException;
import com.devops.common.exception.MvcBusinessException;
import com.devops.common.exception.SimilarNameException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Assert {
	public Assert() {
    }

    public static void isTrue(boolean exp, String message) {
        if (!exp) {
            throw new MvcBusinessException(message);
        }
    }

    public static void isTrueMvc(boolean exp, String message) {
        if (!exp) {
            throw new MvcBusinessException(message);
        }
    }

    public static void isIdCard(String text) {
        if (!RegularUtil.isIdentity(text)) {
            throw new MvcBusinessException("身份证号格式错误！");
        }
    }

    public static void handleData(boolean exp, String message){
        if (!exp){
            throw new HandleDataException(message);
        }
    }

    public static void isSimilar(boolean exp, String message) {
        if (!exp) {
            throw new SimilarNameException(message);
        }
    }

    public static void isMobile(String text) {
        String regex = "((^(13|14|15|16|17|18|19)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
        pattern(text, regex, true, "手机号格式错误！");
    }

    public static void isHttpUrl(String text) {
        String regex = "((http[s]{0,1})://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
    }

    public static void pattern(String text, String regex, boolean flag, String key) {
        boolean result = false;

        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
            result = matcher.matches();
        } catch (Exception var7) {
            result = false;
        }

        if (result != flag) {
            throw new MvcBusinessException(key);
        }
    }
}
